<?php
function catchu($value, $length)
{
	if($value!=''){
		if(is_array($value)) list($string, $match_to) = $value;
		else { $string = $value; $match_to = $value{0}; }

		$match_start = stristr($string, $match_to);
		$match_compute = strlen($string) - strlen($match_start);

		if (strlen($string) > $length)
		{
			if ($match_compute < ($length - strlen($match_to)))
			{
				$pre_string = substr($string, 0, $length);
				$pos_end = strrpos($pre_string, " ");
				if($pos_end === false) $string = $pre_string."...";
				else $string = substr($pre_string, 0, $pos_end)."...";
			}
			else if ($match_compute > (strlen($string) - ($length - strlen($match_to))))
			{
				$pre_string = substr($string, (strlen($string) - ($length - strlen($match_to))));
				$pos_start = strpos($pre_string, " ");
				$string = "...".substr($pre_string, $pos_start);
				if($pos_start === false) $string = "...".$pre_string;
				else $string = "...".substr($pre_string, $pos_start);
			}
			else
			{
				$pre_string = substr($string, ($match_compute - round(($length / 3))), $length);
				$pos_start = strpos($pre_string, " "); $pos_end = strrpos($pre_string, " ");
				$string = "...".substr($pre_string, $pos_start, $pos_end)."...";
				if($pos_start === false && $pos_end === false) $string = "...".$pre_string."...";
				else $string = "...".substr($pre_string, $pos_start, $pos_end)."...";
			}

			$match_start = stristr($string, $match_to);
			$match_compute = strlen($string) - strlen($match_start);
		}
		 
		return $string;
	}else{
		return $string ='';
	}
}
?>
<?php 
$quyen01 = $this->users->getQuyen($this->session->userdata("user_id"));
if(count($quyen01) > 0){
	foreach($quyen01 as $q){
		$quyen[] = $q->id_quyen;
	}
}
else
	$quyen = array();
?>
<div class="content">
        <div class="content_header">
        THÔNG BÁO MỚI
         <?php 
            if(in_array(3, $quyen)) {
         ?>
        <a class="buttonthem" href="<?php echo base_url()?>tintuc/viewadd">Thêm thông báo</a>
        <?php }?>
        </div>
        <!--  -->
        <div class="content_news">
        <?php 
        $first = 1;
        foreach($allnew as $new){
			$tittle = $new->tieude_tintuc;
			$tomtat = catchu(strip_tags($new->noidung_tintuc),225);
			$date = $new->ngay_tintuc;
			$user = $new->tenthat_nguoidung;
			if($first == 1) $first_box = 'first_box';
			else
				$first_box = "";
        ?>
           <div class="news_box first_box">
                <div class="news_box_header">
                    <a href="<?php echo base_url().'tintuc/view/'.$new->id_tintuc?>" class="news_box_header">
                    	<?php echo $tittle;?>
	                </a>
	                <?php 
	                $user_id = $this->session->userdata("user_id");
	                if((in_array(3, $quyen) && $user_id == $new->id_nguoidung) || $user_id=12){
	                ?>
	                <a class="suabutton" href="<?php echo base_url().'tintuc/viewedit/'.$new->id_tintuc?>">Sửa</a>
	                <a class="suabutton" href="<?php echo base_url().'tintuc/del/'.$new->id_tintuc?>">Xóa</a>
	                <?php }?>
                </div>
                <div class="news_box_time">
                     <?php echo $date." - ".$user?>
                </div>
                <div class="news_box_content">
	                <?php echo $tomtat;?>
                </div>
            </div>
         <?php $first = 0;}?> 
         <?php echo $this->pagination->create_links();?>  
         <div style="clear:both"></div>          
 		</div><!-- end the content_news -->
 </div><!-- end the content -->
 </div><!-- end the container -->